package com.hll.erp.config;

import com.fasterxml.classmate.TypeResolver;
import com.hll.erp.entity.Category;
import com.hll.erp.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author Mr.Hu
 * @since 2021-03-18 12:07
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
  @Autowired TypeResolver typeResolver;

  @Value("${swagger.enable}")
  private boolean enable;

  @Bean
  public Docket createRestApi(Environment environment) {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .enable(enable)
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.hll.erp"))
        .paths(PathSelectors.any())
        .build()
        .additionalModels(typeResolver.resolve(Category.class))
        .additionalModels(typeResolver.resolve(User.class));
    // 如果controller里没有requestBody的类参数，相应model就不会显示
  }

  private ApiInfo apiInfo() {
    Contact contact = new Contact("hll", "https://www.hll.com", "haha@qq.com");
    return new ApiInfo(
        "ERP进存销系统",
        "swagger接口文档",
        "v1.0",
        "https://www.hll.com",
        contact,
        "Apache 2.0 许可",
        "许可链接",
        new ArrayList<>());
  }
}
